package com.qyhl.webtv.module_news.news.picturenews;

import android.util.Log;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.PictureBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

/**
 * Created by qyhl on 2017/11/29.
 */

public class PictureNewsModel implements PictureNewsContract.PictureNewsModel {
    private PictureNewsPresenter mPresenter;

    public PictureNewsModel(PictureNewsPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取图片新闻详情
     *
     * @param newsId
     */
    @Override
    public void getDetail(String newsId) {
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.get(NewsUrl.GET_PHOTO_NEWS_DETAIL)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("userName", userName)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("newsId", newsId)
                .execute(new SimpleCallBack<PictureBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(2, "暂无任何内容！");
                        } else {
                            mPresenter.setError(1, "解析出错，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(PictureBean pictureBean) {
                        if (pictureBean != null && pictureBean.getContent() != null && pictureBean.getContent().size() > 0) {
                            mPresenter.setDetail(pictureBean);
                        } else {
                            mPresenter.setError(2, "暂无任何内容！");
                        }
                    }
                });
    }

    /**
     * 新闻点击数统计
     *
     * @param newsId
     */
    @Override
    public void count(String newsId) {

        EasyHttp.get(NewsUrl.GET_NEWS_DETAIL_COUNT)
                .params("newsId", newsId)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s == null) {
                            Log.e("error_count", "新闻点击数返回为空！");
                        } else {
                            Log.i("success_count", "点击数统计成功！");
                        }
                    }
                });
    }

    /**
     * 收藏新闻
     *
     * @param newsId
     */
    @Override
    public void collect(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "collectArticle")
                .params("userName", userName)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("articleID", newsId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.collectError("网络异常，请稍后重试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.collectSuccess("成功收藏");
                        } else {
                            mPresenter.collectError("收藏失败，请稍后重试！");
                        }
                    }
                });
    }

    /**
     * 取消收藏
     *
     * @param newsId
     */
    @Override
    public void cancelCollect(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "deleteCollectArticle")
                .params("userName", userName)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("articleID", newsId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.cancelCollectError("网络异常，请稍后重试!");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.cancelCollectSuccess();
                        } else {
                            mPresenter.cancelCollectError("取消失败，请稍后重试！");
                        }
                    }
                });
    }
}
